package com.iwomy.secureplat.platform.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.data.datascope.DataScope;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.dto.BasicInventoryListDTO;
import com.iwomy.secureplat.platform.entity.*;
import com.iwomy.secureplat.platform.exception.PlatformException;
import com.iwomy.secureplat.platform.mapper.BasicOutInventoryNumberMapper;
import com.iwomy.secureplat.platform.utils.DataScopeUtils;
import com.iwomy.secureplat.platform.utils.RemoteDeptUtils;
import com.iwomy.secureplat.platform.mapper.BasicInventoryListMapper;
import com.iwomy.secureplat.platform.service.*;
import com.iwomy.secureplat.platform.vo.BasicInventoryListVO;
import com.iwomy.secureplat.platform.vo.DeviceInventoryListVO;
import com.iwomy.secureplat.platform.vo.QuerySmsTtsCountVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 出库列表-出库记录
 *
 * @author pig
 * @date 2023-12-12 11:13:45
 */
@Service
public class BasicInventoryListServiceImpl extends ServiceImpl<BasicInventoryListMapper, BasicInventoryListEntity> implements BasicInventoryListService {

	@Autowired
	private BasicExpensesDeptDetailService basicExpensesDeptDetail;

	@Autowired
	private BasicDeviceService basicDeviceService;

	@Autowired
	private BasicExpensesDviceConfigService basicExpensesDviceConfigService;

	@Autowired
	private BasicConsumptionRecordService basicConsumptionRecordService;

	@Autowired
	private RemoteDeptUtils remoteDeptUtils;
	
	@Autowired
	private BasicOutInventoryNumberService basicOutInventoryNumberService;

	@Autowired
	private BasicOutInventoryNumberMapper basicOutInventoryNumberMapper;

	//出库记录
	@Override
	public Page getBasicInventoryListPage(Page page, BasicInventoryListDTO basicInventoryListDTO) {
		LambdaQueryWrapper<BasicOutInventoryNumberEntity> queryWrapper = Wrappers.lambdaQuery();
		List<BasicOutInventoryNumberEntity> list = basicOutInventoryNumberMapper.selectListByScope(queryWrapper, new DataScope());
		List<Long> inventoryListId = list.stream().map(BasicOutInventoryNumberEntity::getId).collect(Collectors.toList());

		if (ObjectUtil.isEmpty(inventoryListId)) return page;

		LambdaQueryWrapper<BasicOutInventoryNumberEntity> wrapper = new LambdaQueryWrapper<>();

		List<?> lists = DataScopeUtils.getList();
		if (lists != null && !lists.isEmpty()) {
			wrapper.in(BasicOutInventoryNumberEntity::getCustomerDeptId, lists);
		}

		if (ArrayUtil.isNotEmpty(basicInventoryListDTO.getCreateTime())) {
			wrapper.ge(BasicOutInventoryNumberEntity::getCreateTime, basicInventoryListDTO.getCreateTime()[0])
					.le(BasicOutInventoryNumberEntity::getCreateTime, basicInventoryListDTO.getCreateTime()[1]);
		}
		if (StrUtil.isNotEmpty(basicInventoryListDTO.getImei())) {
			String imei = basicInventoryListDTO.getImei();  // 获取输入的 IMEI 号
			wrapper.like(BasicOutInventoryNumberEntity::getImei, "%" + imei + "%");  // 使用 % 作为通配符进行模糊查询
		}


		if (StrUtil.isNotEmpty(basicInventoryListDTO.getOutInventoryNumber())) {
			wrapper.like(BasicOutInventoryNumberEntity::getOutInventoryNumber, basicInventoryListDTO.getOutInventoryNumber());
		}

		if (ObjectUtil.isNotEmpty(basicInventoryListDTO.getDeptId())) {
			wrapper.in(BasicOutInventoryNumberEntity::getCustomerDeptId, basicInventoryListDTO.getDeptId());
		}

		wrapper.in(BasicOutInventoryNumberEntity::getId, inventoryListId);

		Page<BasicOutInventoryNumberEntity> basicInventory =basicOutInventoryNumberMapper.selectPage(page, wrapper);

		return basicInventory;
	}

	//配送新增
	@Override
	@Transactional(rollbackFor = Exception.class)
	public R newlyAddedBatch(BasicInventoryListDTO basicInventory) {
		Map<String, Object> data = remoteDeptUtils.getDept(basicInventory.getCustomerDeptId());
		String parentIdString = (String) data.getOrDefault("parentId", null);
		Long parentId = Long.parseLong(parentIdString);

		if (parentId != 0) {
			int deviceRemain = 0;
			QuerySmsTtsCountVO querySmsTtsCount = basicExpensesDeptDetail.queryDeptCount(basicInventory.getCustomerDeptId());
			deviceRemain = querySmsTtsCount.getDeviceRemainNum();
			if (deviceRemain < basicInventory.getDeviceIds().length) return R.failed("下级单位无可用设备数量，出库失败");
		}

		Long[] deviceIds = basicInventory.getDeviceIds();

		List<BasicInventoryListDTO> basicInventoryListEntities = new ArrayList<>();
		String outboundOrderNumber = null;
		int deviceNum = 0;
		for (Long deviceId : deviceIds) {
			BasicInventoryListDTO inventory = new BasicInventoryListDTO();
			BeanUtils.copyProperties(basicInventory, inventory);
			inventory.setDeviceId(deviceId);
			inventory.setCustomerDeptId(basicInventory.getCustomerDeptId());
			inventory.setTransferDeptId(basicInventory.getCustomerDeptId());

			// 生成出库单号
			outboundOrderNumber = generateOutboundOrderNumber();
			inventory.setOutInventoryNumber(outboundOrderNumber);

			basicInventoryListEntities.add(inventory);

			//出库给自己时不需要记录扣除数量
			BasicDeviceEntity deviceEntity = basicDeviceService.getById(deviceId);
			if (!deviceEntity.getDeptId().equals(basicInventory.getCustomerDeptId())){
				deviceNum+=1;
			}
		}

		List<BasicInventoryListDTO> inventoryList = basicInventoryListEntities.stream()
				.filter(basicInventoryList -> basicInventoryList.getIsCommunicationFees() == 10 || basicInventoryList.getIsCommunicationFees() == 20)
				.map(basicInventoryListEntity -> {
					LocalDateTime now = LocalDateTime.now();
					basicInventoryListEntity.setPlatformExpireDate(now.plusYears(basicInventoryListEntity.getPlatformFeeYears()));
					return basicInventoryListEntity;
				}).collect(Collectors.toList());

		List<String> imeis = new ArrayList<>();


		//新增到出库记录中
		List<BasicInventoryListEntity> entities = inventoryList(inventoryList,imeis);
		this.saveBatch(entities);

		//新增到出库单号表中
		String imei = String.join(";", imeis);
		String customerDeptName = remoteDeptUtils.getDeptName(basicInventory.getCustomerDeptId());
		BasicOutInventoryNumberEntity basicOutInventoryNumber = new BasicOutInventoryNumberEntity();
		basicOutInventoryNumber.setOutInventoryNumber(outboundOrderNumber);
		basicOutInventoryNumber.setDeptId(SecurityUtils.getUser().getDeptId());
		basicOutInventoryNumber.setCustomerDeptId(basicInventory.getCustomerDeptId());
		basicOutInventoryNumber.setCustomerDeptName(customerDeptName);
		basicOutInventoryNumber.setDeviceSum(entities.size());
		basicOutInventoryNumber.setOutInventoryType(10);
		basicOutInventoryNumber.setTenantId(SecurityUtils.getUser().getTenantId());
		basicOutInventoryNumber.setImei(imei);
		basicOutInventoryNumberService.save(basicOutInventoryNumber);

		//设备赠送记录为需要配置时
		inventoryList.stream().filter(basicInventoryListEntity -> basicInventoryListEntity.getIsCommunicationFees() == 20)
				.forEach(basicInventoryListEntity -> {
					expensesDeviceConfig(basicInventoryListEntity);
				});

		//出库时配送的短信语音消费记录
		inventoryList.stream().filter(basicInventoryListEntity -> basicInventoryListEntity.getIsCommunicationFees() == 20)
				.forEach(basicInventoryListEntity -> {
					notificationRecord(basicInventoryListEntity);
				});

		//修改设备列表中设备的出库状态改为已出库
		List<BasicDeviceEntity> deviceList = basicDeviceList(inventoryList);

		if (!parentId.equals(0L)) {
			//修改设备资费中的设备剩余数
			basicExpensesDeptDetail.deductDevice(deviceNum, basicInventory.getCustomerDeptId());
			//配送时才扣去出库单位的余额
			if (basicInventory.getDeliveryMethod() == 10) {
				basicExpensesDeptDetail.deptDeductSms(basicInventory.getSmsNum()*inventoryList.size(), SecurityUtils.getUser().getDeptId());
				basicExpensesDeptDetail.deptDeductTts(basicInventory.getTtsNum()*inventoryList.size(), SecurityUtils.getUser().getDeptId());
			}
		}

		basicDeviceService.saveOrUpdateBatch(deviceList);
		return R.ok();
	}

	//生成出库单
	private String generateOutboundOrderNumber() {
		LocalDateTime nowTime = LocalDateTime.now();
		String outInventoryNumber = "";
		BasicInventoryListEntity latestRecord = this.getBaseMapper().findLatestRecord();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
		String dateString = nowTime.format(formatter);
		//如果目前没有出库记录，则当前时间加上8个0
		if (latestRecord == null) {
			return dateString + "00000000";
		} else {
			outInventoryNumber = latestRecord.getOutInventoryNumber();
		}

		// 将序列号字符串转换为BigInteger类型,提取最后八位，逐渐加1
		BigInteger lastSequenceNumber = new BigInteger(outInventoryNumber).mod(BigInteger.valueOf(100000000));
		BigInteger newSequenceNumber = lastSequenceNumber.add(BigInteger.ONE).mod(BigInteger.valueOf(100000000));

		String sequenceString = String.format("%08d", newSequenceNumber);
		return dateString + sequenceString;
	}

	//出库记录到消费记录表
	private void notificationRecord(BasicInventoryListDTO entityList) {
		if (entityList.getDeliveryMethod() == 10) {
			getConsumptionRecordMethod10(entityList);
		} else {
			getConsumptionRecordMethod20(entityList);
		}
	}

	//出库配送记录到消费记录表
	private void getConsumptionRecordMethod10(BasicInventoryListDTO entityList) {
		DeviceInventoryListVO basicDevice = basicDeviceService.getDeviceInventoryList(entityList.getDeviceId());
		if (entityList.getSmsNum() != null) {
			BasicConsumptionRecordEntity basicConsumptionRecord = new BasicConsumptionRecordEntity();
			basicConsumptionRecord.setDeptId(SecurityUtils.getUser().getDeptId());
			basicConsumptionRecord.setDeviceId(entityList.getDeviceId());
			basicConsumptionRecord.setUsedType(30);
			basicConsumptionRecord.setNotificationType(10);
			basicConsumptionRecord.setSmsNumber(entityList.getSmsNum());
			basicConsumptionRecord.setCustomerDeptId(entityList.getCustomerDeptId());
			getDevice(basicConsumptionRecord,basicDevice);
			basicConsumptionRecordService.save(basicConsumptionRecord);
		}
		if (entityList.getTtsNum() != null) {
			BasicConsumptionRecordEntity basicConsumptionRecord = new BasicConsumptionRecordEntity();
			basicConsumptionRecord.setDeptId(SecurityUtils.getUser().getDeptId());
			basicConsumptionRecord.setDeviceId(entityList.getDeviceId());
			basicConsumptionRecord.setUsedType(30);
			basicConsumptionRecord.setNotificationType(20);
			basicConsumptionRecord.setTtsNumber(entityList.getTtsNum());
			basicConsumptionRecord.setCustomerDeptId(entityList.getCustomerDeptId());
			getDevice(basicConsumptionRecord,basicDevice);
			basicConsumptionRecordService.save(basicConsumptionRecord);
		}
	}

	//出库代付记录到消费记录表
	private void getConsumptionRecordMethod20(BasicInventoryListDTO entityList) {
		DeviceInventoryListVO basicDevice = basicDeviceService.getDeviceInventoryList(entityList.getDeviceId());
		if (entityList.getSmsTotalLimit() != null) {
			BasicConsumptionRecordEntity basicConsumptionRecord = new BasicConsumptionRecordEntity();
			basicConsumptionRecord.setDeptId(SecurityUtils.getUser().getDeptId());
			basicConsumptionRecord.setDeviceId(entityList.getDeviceId());
			basicConsumptionRecord.setUsedType(40);
			basicConsumptionRecord.setNotificationType(10);
			basicConsumptionRecord.setSmsNumber(entityList.getSmsTotalLimit());
			basicConsumptionRecord.setCustomerDeptId(entityList.getCustomerDeptId());
			getDevice(basicConsumptionRecord,basicDevice);
			basicConsumptionRecordService.save(basicConsumptionRecord);
		}
		if (entityList.getTtsTotalLimit() != null) {
			BasicConsumptionRecordEntity basicConsumptionRecord = new BasicConsumptionRecordEntity();
			basicConsumptionRecord.setDeptId(SecurityUtils.getUser().getDeptId());
			basicConsumptionRecord.setDeviceId(entityList.getDeviceId());
			basicConsumptionRecord.setUsedType(40);
			basicConsumptionRecord.setNotificationType(20);
			basicConsumptionRecord.setTtsNumber(entityList.getTtsTotalLimit());
			basicConsumptionRecord.setCustomerDeptId(entityList.getCustomerDeptId());
			getDevice(basicConsumptionRecord,basicDevice);
			basicConsumptionRecordService.save(basicConsumptionRecord);
		}
	}

	//根据设备id查看赠送记录
	@Override
	public BasicInventoryListEntity getInventoryList(Long deviceId) {
		LambdaQueryWrapper<BasicInventoryListEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.in(BasicInventoryListEntity::getDeviceId, deviceId);
		BasicInventoryListEntity basicInventory = baseMapper.selectOne(wrapper);

		return basicInventory;
	}

	//查询租户使用设备数
	@Override
	public Integer tenantDeptDeviceNum(Long deptId) {
		//别人的设备出库给我的数量
		QueryWrapper<BasicInventoryListEntity> wrapper = new QueryWrapper<>();
		wrapper.eq("customer_dept_id", deptId);
		wrapper.ne("create_dept_id", deptId);
		wrapper.between("create_time",
				LocalDateTime.of(LocalDate.now().withDayOfMonth(1), LocalTime.MIN),
				LocalDateTime.of(LocalDate.now().withDayOfMonth(LocalDate.now().lengthOfMonth()), LocalTime.MAX)
		);
		Long inventoryCount = this.getBaseMapper().selectCount(wrapper);

		//我的设备出库的数量
		QueryWrapper<BasicInventoryListEntity> wrapper2 = new QueryWrapper<>();
		wrapper2.eq("create_dept_id", deptId);
		wrapper2.between("create_time",
				LocalDateTime.of(LocalDate.now().withDayOfMonth(1), LocalTime.MIN),
				LocalDateTime.of(LocalDate.now().withDayOfMonth(LocalDate.now().lengthOfMonth()), LocalTime.MAX)
		);
		Long inventoryCount2 = this.getBaseMapper().selectCount(wrapper2);

		//我的设备还未出库的数量
		QueryWrapper<BasicDeviceEntity> deviceWrapper = new QueryWrapper<>();
		deviceWrapper.eq("create_dept_id", deptId);
		deviceWrapper.eq("stocks", 10);
		deviceWrapper.between("create_time",
				LocalDateTime.of(LocalDate.now().withDayOfMonth(1), LocalTime.MIN),
				LocalDateTime.of(LocalDate.now().withDayOfMonth(LocalDate.now().lengthOfMonth()), LocalTime.MAX)
		);

		Long deviceCount = basicDeviceService.count(deviceWrapper);

		Integer thisMonthDeviceTotal = inventoryCount.intValue() + deviceCount.intValue()+inventoryCount2.intValue();
		return thisMonthDeviceTotal;
	}

	//根据设备id查询设备对应信息
	private BasicInventoryListVO inventoryListVO(BasicInventoryListEntity
														 entityList, Map<String, Integer> deviceSumMap) {
		BasicInventoryListVO basicInventoryListVO = new BasicInventoryListVO();
		BeanUtils.copyProperties(entityList, basicInventoryListVO);
		Long deviceId = entityList.getDeviceId();
		String deptName = remoteDeptUtils.getDeptName(entityList.getCustomerDeptId());
		basicInventoryListVO.setCustomerDeptName(deptName);
		DeviceInventoryListVO basicDevice = basicDeviceService.getDeviceInventoryList(deviceId);
		if (basicDevice != null) {
			basicInventoryListVO.setDeviceType(basicDevice.getDeviceType());
			basicInventoryListVO.setDeviceModelName(basicDevice.getDeviceModelName());
			basicInventoryListVO.setImei(basicDevice.getImei());
		}
		basicInventoryListVO.setDeviceSum(deviceSumMap.get(entityList.getOutInventoryNumber()));
		return basicInventoryListVO;
	}

	//通过出库单号查询设备出库详情
	@Override
	public BasicInventoryListVO getoutInventoryNumber(String outInventoryNumber) {
		LambdaQueryWrapper<BasicInventoryListEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.in(BasicInventoryListEntity::getOutInventoryNumber, outInventoryNumber);
		List<BasicInventoryListEntity> basicInventoryListEntities = baseMapper.selectList(wrapper);

		BasicInventoryListVO basicInventoryListVO = new BasicInventoryListVO();
		List<DeviceInventoryListVO> voList = new ArrayList<>();
		Set<Long> uniqueDeviceIds = new HashSet<>();

		for (BasicInventoryListEntity inventoryList : basicInventoryListEntities) {
			if (!uniqueDeviceIds.contains(inventoryList.getDeviceId())) {
				uniqueDeviceIds.add(inventoryList.getDeviceId());
				if (basicInventoryListVO.getId() == null) {
					BeanUtils.copyProperties(inventoryList, basicInventoryListVO);
				}
				DeviceInventoryListVO basicDeviceVO = basicDeviceService.getDeviceInventoryList(inventoryList.getDeviceId());
				if (basicDeviceVO != null) {
					voList.add(basicDeviceVO);
				} else {
					DeviceInventoryListVO deviceInventoryListVO = new DeviceInventoryListVO();
					deviceInventoryListVO.setDeviceType(inventoryList.getDeviceTypeName());
					deviceInventoryListVO.setDeviceModelName(inventoryList.getDeviceModelName());
					deviceInventoryListVO.setImei(inventoryList.getImei());
					voList.add(deviceInventoryListVO);
				}
			}
		}

		String customerDeptName = remoteDeptUtils.getDeptName(basicInventoryListVO.getCustomerDeptId());
		String deptName = remoteDeptUtils.getDeptName(basicInventoryListVO.getDeptId());
		basicInventoryListVO.setCustomerDeptName(customerDeptName);
		basicInventoryListVO.setDeptName(deptName);
		basicInventoryListVO.setBasicDevice(voList);

		return basicInventoryListVO;
	}

	//新增写入到出库记录中
	private List<BasicInventoryListEntity> inventoryList(List<BasicInventoryListDTO> inventoryList,List<String> imeis) {
		QuerySmsTtsCountVO querySmsTtsCount = basicExpensesDeptDetail.queryDeptCount(SecurityUtils.getUser().getDeptId());
		int smsRemain = querySmsTtsCount.getSmsRemainNum();
		int ttsRemain = querySmsTtsCount.getTtsRemainNum();

		Map<String, Object> data = remoteDeptUtils.getDept(SecurityUtils.getUser().getDeptId());
		String parentIdString = (String) data.getOrDefault("parentId", null);
		Long parentId = Long.parseLong(parentIdString);

		List<BasicInventoryListEntity> entities = inventoryList.stream().map(dto -> {
			if (parentId != 0) {
				if (smsRemain < dto.getSmsNum() ||
						ttsRemain < dto.getTtsNum()) {
					throw new PlatformException("短信/语音/剩余量不足！");
				}
			}

			BasicInventoryListEntity entity = new BasicInventoryListEntity();
			BeanUtils.copyProperties(dto, entity);
			//如果为配送，则刚出库时的设备语音短信剩余和出库时设置的数一样
			if (dto.getDeliveryMethod() == 10) {
				entity.setSmsRemainNum(dto.getSmsNum());
				entity.setTtsRemainNum(dto.getTtsNum());
			} else {
				//如果为代付，则刚出库时的设备语音短信剩余和出库时的代付设置的数一样
				entity.setSmsNum(dto.getSmsTotalLimit());
				entity.setTtsNum(dto.getTtsTotalLimit());
				entity.setSmsRemainNum(dto.getSmsTotalLimit());
				entity.setTtsRemainNum(dto.getTtsTotalLimit());
			}
			DeviceInventoryListVO basicDevice = basicDeviceService.getDeviceInventoryList(entity.getDeviceId());
			entity.setCreateDeptId(basicDevice.getDeptId());
			entity.setImei(basicDevice.getImei());
			entity.setDeviceTypeName(basicDevice.getDeviceType());
			entity.setDeviceModelName(basicDevice.getDeviceModelName());
			entity.setPlatformYears(dto.getPlatformFeeYears());
			entity.setDeptId(SecurityUtils.getUser().getDeptId());
			imeis.add(basicDevice.getImei());
			return entity;
		}).collect(Collectors.toList());

		return entities;
	}

	//设备的出库状态改变
	private List<BasicDeviceEntity> basicDeviceList(List<BasicInventoryListDTO> inventoryList) {
		return inventoryList.stream()
				.map(basicInventoryList -> {
					Long deviceId = basicInventoryList.getDeviceId();
					BasicDeviceEntity basicDeviceEntity = basicDeviceService.getById(deviceId);
					if (basicDeviceEntity != null) {
						basicDeviceEntity.setBindState(20);
						basicDeviceEntity.setStocks(20);
						basicDeviceEntity.setDeptId(basicInventoryList.getCustomerDeptId());
					}
					return basicDeviceEntity;
				})
				.filter(Objects::nonNull)
				.collect(Collectors.toList());
	}


	//设备赠送记录
	private void expensesDeviceConfig(BasicInventoryListDTO inventoryList) {
		if (inventoryList.getDeliveryMethod() == 10) {
			if (inventoryList.getSmsNum() != 0) getDeliveryMethod10Sms(inventoryList, 10);
			if (inventoryList.getTtsNum() != 0) getDeliveryMethod10Tts(inventoryList, 20);

		} else {
			if (inventoryList.getSmsTotalLimit() != 0) getDeliveryMethod20Sms(inventoryList, 10);
			if (inventoryList.getTtsTotalLimit() != 0) getDeliveryMethod20Tts(inventoryList, 20);

		}
	}

	//出库配置方式：配送短信
	private void getDeliveryMethod10Sms(BasicInventoryListDTO inventoryList, int type) {
		BasicExpensesDviceConfigEntity deviceConfig = getDeviceConfig(inventoryList, type);
		deviceConfig.setDeliveryNum(inventoryList.getSmsNum());
		deviceConfig.setRemainNum(inventoryList.getSmsNum());
		deviceConfig.setUseRemainNum(inventoryList.getSmsNum());
		basicExpensesDviceConfigService.save(deviceConfig);
	}

	//出库配置方式：配送语音
	private void getDeliveryMethod10Tts(BasicInventoryListDTO inventoryList, int type) {
		BasicExpensesDviceConfigEntity deviceConfig = getDeviceConfig(inventoryList, type);
		deviceConfig.setDeliveryNum(inventoryList.getTtsNum());
		deviceConfig.setRemainNum(inventoryList.getTtsNum());
		deviceConfig.setUseRemainNum(inventoryList.getTtsNum());
		basicExpensesDviceConfigService.save(deviceConfig);
	}

	//出库配置方式：代付短信
	private void getDeliveryMethod20Sms(BasicInventoryListDTO inventoryList, int type) {
		BasicExpensesDviceConfigEntity deviceConfig = getDeviceConfig(inventoryList, type);
		deviceConfig.setTotalLimit(inventoryList.getSmsTotalLimit());
		deviceConfig.setSumDay(inventoryList.getSmsSumDay());
		deviceConfig.setSumMonth(inventoryList.getSmsSumMonth());
		deviceConfig.setDeliveryNum(inventoryList.getSmsTotalLimit());
		deviceConfig.setRemainNum(inventoryList.getSmsTotalLimit());
		deviceConfig.setUseRemainNum(inventoryList.getSmsTotalLimit());
		deviceConfig.setTotalLimitRemain(inventoryList.getSmsTotalLimit());
		deviceConfig.setSumDayRemain(inventoryList.getSmsSumDay());
		deviceConfig.setSumMonthRemain(inventoryList.getSmsSumMonth());
		deviceConfig.setExpirationDate(inventoryList.getExpirationDate());
		basicExpensesDviceConfigService.save(deviceConfig);
	}

	//出库配置方式：代付语音
	private void getDeliveryMethod20Tts(BasicInventoryListDTO inventoryList, int type) {
		BasicExpensesDviceConfigEntity deviceConfig = getDeviceConfig(inventoryList, type);
		deviceConfig.setTotalLimit(inventoryList.getTtsTotalLimit());
		deviceConfig.setSumDay(inventoryList.getTtsSumDay());
		deviceConfig.setSumMonth(inventoryList.getTtsSumMonth());
		deviceConfig.setDeliveryNum(inventoryList.getTtsTotalLimit());
		deviceConfig.setRemainNum(inventoryList.getTtsTotalLimit());
		deviceConfig.setUseRemainNum(inventoryList.getTtsTotalLimit());
		deviceConfig.setTotalLimitRemain(inventoryList.getTtsTotalLimit());
		deviceConfig.setSumDayRemain(inventoryList.getTtsSumDay());
		deviceConfig.setSumMonthRemain(inventoryList.getTtsSumMonth());
		deviceConfig.setExpirationDate(inventoryList.getExpirationDate());
		basicExpensesDviceConfigService.save(deviceConfig);
	}

	//短信语音分别写进资费设备配置记录表中
	private BasicExpensesDviceConfigEntity getDeviceConfig(BasicInventoryListDTO inventoryList, int type) {
		DeviceInventoryListVO basicDevice = basicDeviceService.getDeviceInventoryList(inventoryList.getDeviceId());
		BasicExpensesDviceConfigEntity deviceConfig = new BasicExpensesDviceConfigEntity();
		deviceConfig.setType(type);
		deviceConfig.setDeviceId(inventoryList.getDeviceId());
		deviceConfig.setDeptId(inventoryList.getCustomerDeptId());
		deviceConfig.setGiveDeptId(SecurityUtils.getUser().getDeptId());
		deviceConfig.setDeliveryMethod(inventoryList.getDeliveryMethod());
		deviceConfig.setDeviceType(basicDevice.getDeviceType());
		deviceConfig.setDeviceModelName(basicDevice.getDeviceModelName());
		deviceConfig.setImei(basicDevice.getImei());
		return deviceConfig;
	}

	private BasicConsumptionRecordEntity getDevice(BasicConsumptionRecordEntity basicConsumptionRecord,DeviceInventoryListVO basicDevice){
		basicConsumptionRecord.setDeviceType(basicDevice.getDeviceType());
		basicConsumptionRecord.setDeviceModelName(basicDevice.getDeviceModelName());
		basicConsumptionRecord.setImei(basicDevice.getImei());
		return basicConsumptionRecord;
	}

}
